// pages/carrierUser/carrierMe/index.js
const {
  $Message
} = require('../../dist/base/index');
var api = require('../../utils/api.js')
const user = require('../index/model.js')
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    funcArr:["投递记录","我的信息","问题反馈","账户余额","切换角色","关于"],
    userInfo: {},
    time_range:"一月内",
    historyArr:[],
    time_day:"",
    is_later:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取当前时间戳  
    var timestamp = Date.parse(new Date());  
    timestamp = timestamp;  
    console.log("当前时间戳为：" + timestamp);  
    //获取当前时间  
    var n = timestamp;  
    var date = new Date(n);  
    //年  
    var Y = date.getFullYear();  
    //月  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);  
    //日  
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
    //时  
    var h = date.getHours();  
    //分  
    var m = date.getMinutes();  
    //秒  
    var s = date.getSeconds();
    //默认前推一个月
    var frontM = parseInt(M) - 1
    var frontMString = frontM > 9 ? frontM + '' : '0' + frontM
    var time_day = Y + frontMString + D
    console.log(time_day)
    this.setData({
      time_day:time_day
    })
    this.refreshHistory()
  },
  refreshHistory(){
    var self = this
    this.setData({
      spinShow:true
    })
    const _ = api.db.command
    var searchJson = {
      user_info_id:user.user_info._id,
      time_day:_.gt(this.data.time_day),
      
    }
    this.data.is_later ? searchJson.is_later = true:null
    //获取签到记录
    this.getHistory(0,[],searchJson)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  click_func: function (detail) {
    console.log(detail.currentTarget.id)
  },
  getHistory(count, historyArr,whereDic) {
    var self = this
    var func = null
    if (count == 0) {
      func = api.db.collection('user_sign_history')
        .limit(20).where(whereDic).orderBy('timestamp', 'desc')
    } else {
      func = api.db.collection('user_sign_history')
        .skip(count)
        .limit(20).where(whereDic).orderBy('timestamp', 'desc')
    }
    //获取单位信息
    func.get({
      success: function (res) {
        // res.data 包含该记录的数据
        console.log(res.data)
        if (res.data.length != 0) {
          historyArr = historyArr.concat(res.data)
          console.log(historyArr)
          count += 20
          self.getHistory(count, historyArr,whereDic)
        } else {
          console.log("结束")
          historyArr.forEach(element => {
            element.timestamp = util.formatTime(new Date(element.timestamp))
          });
          self.setData({
            historyArr: historyArr,
            spinShow:false
          })
        }
      }
    })
  },
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var time_day = e.detail.value.replace('-','')
    this.setData({
      time_range:e.detail.value + ' 之后',
      time_day:time_day
    })
    this.refreshHistory()
  },
  changeLaterState(){
    this.setData({
      is_later:!this.data.is_later
    })
    this.refreshHistory()
  }
})